import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution811 {
    public List<String> subdomainVisits(String[] cpdomains) {
        Map<String,Integer> map=new HashMap<>();
        for (String cpdomain : cpdomains) {
            String[] strings=cpdomain.split(" ");
            int t=Integer.parseInt(strings[0]);
            String[] strings1=strings[1].split("\\.");
            StringBuilder stringBuilder=new StringBuilder();
            for (int i=strings1.length-1;i>=0;i--){
                stringBuilder.insert(0,strings1[i]);
                map.put(stringBuilder.toString(),map.getOrDefault(stringBuilder.toString(),0)+t);
                stringBuilder.insert(0,'.');
            }
        }
        List<String> list=new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            list.add(String.valueOf(entry.getValue()).concat(" ").concat(entry.getKey()));
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(new Solution811().subdomainVisits(new String[]{"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}));
    }
}
